package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.SecurityUtils;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.mall.MallSetmealNorms;
import com.tencreat.service.mall.IMallSetmealNormsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 店铺商品 套餐参数Controller
 *
 * @author tencreat
 * @date 2022-01-08
 */
@Api(tags="店铺商品 套餐参数api")
@RestController
@RequestMapping("/system/setmealNorms")
public class AdminMalSetmealNormsController extends BaseController {
    @Autowired
    private IMallSetmealNormsService mallSetmealNormsService;

    /**
     * 店铺商品 套餐参数列表  店铺详情
     */
    @ApiOperation(value="查询店铺商品 套餐参数列表")
    @PreAuthorize("@ss.hasPermi('system:setmeal_norms:list')")
    @PostMapping("/list")
    public HttpResult<IPage<MallSetmealNorms>> list(@RequestBody  MallSetmealNorms mallSetmealNorms) {
        QueryWrapper<MallSetmealNorms> wrapper = Wrappers.query(mallSetmealNorms);
        wrapper.orderByAsc("sort_order");
        Page<MallSetmealNorms> page =  new Page<>(mallSetmealNorms.getPageNum(), mallSetmealNorms.getPageSize());
        return HttpResult.ok(mallSetmealNormsService.page(page, wrapper));
    }

    @ApiOperation(value="查询店铺商品 套餐参数列表")
    @PreAuthorize("@ss.hasPermi('system:setmeal_norms:list')")
    @PostMapping("/allList")
    public HttpResult<List<MallSetmealNorms>> allList(@RequestBody MallSetmealNorms mallSetmealNorms) {
        List<MallSetmealNorms> list = mallSetmealNormsService.lambdaQuery()
                .eq(MallSetmealNorms::getGoodsId, mallSetmealNorms.getGoodsId())
                .orderByAsc(MallSetmealNorms::getSortOrder).list();
        return HttpResult.ok(list);
    }

    @PostMapping("/allListByAdmin")
    public HttpResult<List<MallSetmealNorms>> allListByAdmin(@RequestBody MallSetmealNorms mallSetmealNorms) {
        List<MallSetmealNorms> list = mallSetmealNormsService.lambdaQuery()
                .eq(MallSetmealNorms::getGoodsId, mallSetmealNorms.getGoodsId())
                .orderByAsc(MallSetmealNorms::getSortOrder).list();
        return HttpResult.ok(list);
    }

    /**
     * 导出店铺商品 套餐参数列表
     */
    @ApiOperation(value="导出店铺商品 套餐参数列表")
    @PreAuthorize("@ss.hasPermi('system:setmeal_norms:export')")
    @Log(title = "店铺商品 套餐参数参数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(MallSetmealNorms mallSetmealNorms) {
        List<MallSetmealNorms> list = mallSetmealNormsService.selectMallSetmealNormsList(mallSetmealNorms);
        ExcelUtil<MallSetmealNorms> util = new ExcelUtil<MallSetmealNorms>(MallSetmealNorms.class);
        return util.exportExcel(list, "setmeal_norms");
    }

    /**
     * 获取店铺商品 套餐参数详细信息
     */
    @ApiOperation(value="获取店铺商品 套餐参数详细信息")
    @PreAuthorize("@ss.hasPermi('system:setmeal_norms:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallSetmealNorms> getInfo(@RequestBody   ReqIdParam id) {
        return HttpResult.ok(mallSetmealNormsService.selectMallSetmealNormsById(id));
    }

    /**
     * 新增店铺商品 套餐参数
     */
    @ApiOperation(value="新增店铺商品 套餐参数")
    @PreAuthorize("@ss.hasPermi('system:setmeal_norms:add')")
    @Log(title = "店铺商品 套餐参数", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody  MallSetmealNorms mallSetmealNorms) {
        mallSetmealNorms.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        return to(mallSetmealNormsService.insertMallSetmealNorms(mallSetmealNorms));
    }

    /**
     * 修改店铺商品 套餐参数
     */
    @ApiOperation(value="修改店铺商品 套餐参数")
    @PreAuthorize("@ss.hasPermi('system:setmeal_norms:edit')")
    @Log(title = "店铺商品 套餐参数", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody  MallSetmealNorms mallSetmealNorms) {
        return to(mallSetmealNormsService.updateMallSetmealNorms(mallSetmealNorms));
    }

    /**
     * 删除店铺商品 套餐参数
     */
    @ApiOperation(value="删除店铺商品 套餐参数")
    @PreAuthorize("@ss.hasPermi('system:setmeal_norms:remove')")
    @Log(title = "店铺商品 套餐参数", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody  ReqIdsParam ids) {
        return to(mallSetmealNormsService.deleteMallSetmealNormsByIds(ids));
    }


    /*****
     *  将当前分类的排序减1 顺序上升一位
     * @param mallSetmealNorms
     * @return
     */
    @PostMapping("/reSortOrderReduce")
    public HttpResult<Integer> reSortOrderReduce(@RequestBody MallSetmealNorms mallSetmealNorms){
        return HttpResult.ok(mallSetmealNormsService.reSortOrderReduce(mallSetmealNorms));
    }

    /*****
     * 将当前分类的排序加1 顺序下降一位
     * @param mallSetmealNorms
     * @return
     */
    @PostMapping("/reSortOrderPlus")
    public HttpResult<Integer> reSortOrderPlus(@RequestBody MallSetmealNorms mallSetmealNorms){
        return HttpResult.ok(mallSetmealNormsService.reSortOrderPlus(mallSetmealNorms));
    }


}
